﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VAdvantage.Classes;
using VAdvantage.Common;
//using ViennaAdvantage.Process;
//using System.Windows.Forms;
//using ViennaAdvantage.Model;
using VAdvantage.DataBase;
using VAdvantage.SqlExec;
using VAdvantage.Utility;
using System.Data;
using VAdvantage.Logging;
//using VAdvantage.Controller;
using VAdvantage.Model;
using VAdvantage.Grids;


namespace VAdvantage.Model
{
    public class CalloutProductToOpportunity : CalloutEngine
    {

        public String ProductInfo(Context ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
        {
            if (value == DBNull.Value || value == null || value.ToString() == "")
            {
                return "";
            }

            
           // ViennaAdvantage.Model.X_C_ProjectLine oppLine = new ViennaAdvantage.Model.X_C_ProjectLine(ctx, Util.GetValueOfInt(mTab.GetRecord_ID()), null);
            VAdvantage.Model.X_M_Product product = new VAdvantage.Model.X_M_Product(ctx, Util.GetValueOfInt(value), null);
            int id = Util.GetValueOfInt(mTab.GetValue("C_ProjectTask_ID"));
            string Sql = "select c_project_id from C_ProjectPhase where C_ProjectPhase_id in(select C_ProjectPhase_id from" +
                        " C_ProjectTask where C_ProjectTask_id=" + id + ")";
            int projID = Util.GetValueOfInt( DB.ExecuteScalar(Sql,null,null));

            if (id == 0)
            {
                projID = Util.GetValueOfInt(mTab.GetValue("C_Project_ID"));

            }

        
            X_C_Project proj = new X_C_Project(ctx, projID, null);
            //String query = "Select M_ProductPrice_id from M_ProductPrice where M_Product=" + Util.GetValueOfInt(value);
            //int priceID = Util.GetValueOfInt(DB.ExecuteScalar(query,null,null)); 
            string query = "Select PriceList from M_ProductPrice where M_PriceList_Version_ID=" + proj.GetM_PriceList_Version_ID() + " and M_Product_id=" + Util.GetValueOfInt(value);
            //ViennaAdvantage.Model.X_M_ProductPrice ProPrice = new ViennaAdvantage.Model.X_M_ProductPrice(ctx, proj.GetM_PriceList_Version_ID(), null);
            decimal PriceList= Util.GetValueOfDecimal(DB.ExecuteScalar(query, null, null));
            mTab.SetValue("PriceList", PriceList);
           // oppLine.SetPriceList(Util.GetValueOfDecimal(PriceList));

            query = "Select PriceStd from M_ProductPrice where M_PriceList_Version_ID=" + proj.GetM_PriceList_Version_ID() + " and M_Product_id=" + Util.GetValueOfInt(value);
            decimal PriceStd= Util.GetValueOfDecimal(DB.ExecuteScalar(query, null, null));
            mTab.SetValue("PlannedPrice", PriceStd);
           // oppLine.SetPlannedPrice(PriceStd);

            mTab.SetValue("PlannedQty", 1);
           // oppLine.SetPlannedQty(1);

            query="Select PriceLimit from M_ProductPrice where M_PriceList_Version_ID=" + proj.GetM_PriceList_Version_ID() + " and M_Product_id=" + Util.GetValueOfInt(value);
            decimal PriceLimit= Util.GetValueOfDecimal(DB.ExecuteScalar(query, null, null));
            decimal discount;
            try
            {

                discount = (Decimal.Subtract(PriceList, PriceStd) * 100) / PriceList;
            }

            catch
            {
            
                return "PriceListNotSelected";
            }

           mTab.SetValue("Discount", Math.Round(discount,2));
           // oppLine.SetDiscount(Decimal.Subtract(PriceList ,PriceStd));

           mTab.SetValue("PlannedMarginAmt", Decimal.Subtract(PriceStd, PriceLimit));
          // oppLine.SetPlannedMarginAmt( Decimal.Subtract(PriceStd, PriceLimit));




            return "";
        }






    }
}
